home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10817 / Showit.c
Encoding:
C/C++ Source or Header  |  1996-08-05  |  8.2 KB  |  255 lines

  1. /*-----------------------------------------------------------------------
  2. AUTHOR       : Greg Scratchley
  3. Program Name : SHOWIT
  4. Version Num  : v1.1 released.
  5. Program Files: Showit.c
  6.            Showit.exe
  7. Program Desc.: Showit is designed to display a text file given the name
  8.         and path of the textfile to be viewed.  The program
  9.         allows scrolling forward and backward through the doc.
  10.         at the users discretion. Ansi.sys must be loaded for this
  11.         program to work correctly.
  12.  
  13. This program is cardware.  If you like it and think it merits a pat on 
  14. my widdle back, send me a postcard!  
  15.  
  16.         G Scratchley
  17.         65 Glamorgan Drive
  18.         Sherwood Park Alberta 
  19.         Canada
  20.  
  21.         T8A 2Y5 
  22.  
  23. -----------------------------------------------------------------------*/
  24.  
  25.  
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29. #define  MAX  200
  30.  
  31.  
  32. int readfile (char filen[]);
  33.  
  34. /**************************************************************************
  35.  
  36. Function Name: main
  37. Parameters   : int argv, char argv
  38. Return Value : error code 0 to dos
  39. Function     : accepts command line entry and acts as command shell for all
  40.            subroutines
  41.  
  42. **************************************************************************/
  43.  
  44. int main(int argc, char *argv[])
  45.     {
  46.     char filen [80];
  47.     int errorlevel=0;
  48.     char errorstring[75];
  49.     int frcount=0;
  50.     if (argc!=2)                   /* if command line strings not = 2   */
  51.        {
  52.        printf("\n\nUsage  \33[40;33;1mC:\SHOWIT a:\\test.txt\33[40;37;0m\n\n");
  53.        printf("\nIf the above line looked distorted, please include the following in config.sys\n\n");
  54.        printf("---------->      device=c:\\dos\\ansi.sys         \n\n\n");
  55.        return 0;
  56.        }
  57.     sscanf(argv[1],"%s",&filen);   /* scan cmdlin string 2 into filen   */
  58.     errorlevel=readfile(filen);    /* retrieve errorlevel from readfile */
  59.                    /* passing the filename from cmdlin  */
  60.     fflush(stdin);
  61.  
  62.     printf("\n\n\n\33[40;31;1mError Log\33[40;37;0m > \33[40;33;1m");
  63.     if (errorlevel==0)
  64.        {
  65.        printf("No errors - user exited at end of document\n\n");
  66.        }
  67.     if (errorlevel==1)
  68.        {
  69.        printf("Error retrieving file - Check the Path\n\n");
  70.        }
  71.     if (errorlevel==2)
  72.        {
  73.        printf("No errors - user exited before end of document\n\n");
  74.        }
  75.     printf("\33[37;44;1m");
  76.     printf("\n");
  77.     printf("\xC9");
  78.     for (frcount=0;frcount<=77;frcount++)
  79.     printf("\xcd");
  80.     printf("\xbb");
  81.     printf("\xba                                                                              \xba");
  82.     printf("\xba   Thanks for using \33[44;33;1mSoftware from Scratch\33[37;44;0m\33[37;44;1m! If you find this program useful    \xba");
  83.     printf("\xba         please drop a postcard to a college student in distress...           \xba");
  84.     printf("\xba                                                                              \xba");
  85.     printf("\xba                           \33[31;44;1mSoftware From Scratch\33[44;37;1m                              \xba");
  86.     printf("\xba                             \33[31;44;1m65 Glamorgan Drive\33[44;37;1m                               \xba");
  87.     printf("\xba                           \33[31;44;1mSherwood Park, Alberta\33[44;37;1m                             \xba");
  88.     printf("\xba                                  \33[31;44;1mT8A 2Y5\33[44;37;1m                                     \xba");
  89.     printf("\xba                                                                              \xba");
  90.     printf("\xba                               P.S.  Thanks L.P!                              \xba");
  91.     printf("\xC8");
  92.     for (frcount=0;frcount<=77;frcount++)
  93.     printf("\xcd");
  94.     printf("\xBC");
  95.     printf("\33[40;37;0m");
  96.     fflush(stdin);
  97.     return 0;
  98.     }
  99.  
  100. /**************************************************************************
  101. Function Name:readfile
  102. Parameters   :char filen (filename from main)
  103. Return Value :integer for errorlevel
  104. Function     :reads the file and displays it one screen at a time, forward
  105.           and backward at the users discretion.
  106.  
  107. **************************************************************************/
  108.  
  109.  
  110.  
  111. int readfile (char filen[])
  112.     {
  113.     int loopcount=0;
  114.     int pagecount=1;
  115.     FILE *infile;                  /* associates pointer to infile from */
  116.                    /* FILE stream                       */
  117.     fpos_t current[MAX];           /* initializes pointer current[] for */
  118.                    /* file position data                */
  119.     int cntchar=0;                 /* Inits for runtime var's           */
  120.     int lincount=0;
  121.     int count=0;
  122.     char charstr;
  123.     int doneptr=0;
  124.     int errorlevel;
  125.     char readdir;
  126.     printf("\33[2J");
  127.     infile=fopen(filen,"r");       /* specifies value of infile as      */
  128.                    /* file filen                        */
  129.     fgetpos(infile,¤t[count]); /* If file not located show error  */
  130.     if (infile==NULL)
  131.        {
  132.        printf("\n\n\n        FILE NOT FOUND! \n\n  ");
  133.        errorlevel=1;
  134.        return (errorlevel);
  135.        }
  136.     do                             /* For each char returned check for  */
  137.        {
  138.        charstr = fgetc(infile);
  139.        if (charstr=='\n')      /* Check for CR */
  140.           {
  141.           lincount++;
  142.           cntchar=0;
  143.           }
  144.        if (cntchar==80)        /* Check to see if line is full      */
  145.           {
  146.           lincount++;
  147.           cntchar=0;
  148.           }
  149.        if (lincount==23)       /* check to see if page is full      */
  150.           {
  151.           cntchar=0;
  152.           lincount=0;
  153.           printf("\n\33[34;40;1mCurrent File: \33[31;40;1m %s \33[40;37;0m",filen);
  154.           printf("\n\33[37;44;1m\xba Software from Scratch \xba FWD <CR> \xba REV <-> \xba QUIT <q> \xba Page %d \xba Choice?:\33[40;37;0m",pagecount);
  155.           fflush(stdin);
  156.           readdir=getchar();
  157.           printf("\33[2J");
  158.        switch (readdir)       /* Switch for user input             */
  159.            {
  160.           case '\n':          /* if enter is pressed*/
  161.          {
  162.          count++;         /* inc count and store new page ptr  */
  163.          pagecount++;
  164.          fgetpos(infile,¤t[count]);
  165.          cntchar=0;
  166.          break;
  167.          }
  168.           case '-' :          /* If minus sign is pressed          */
  169.          {
  170.          if (count==0)    /* dec counter (if not zero already) */
  171.             {
  172.             count++;      /* & set ptr to previous value stored*/
  173.             pagecount++;
  174.             }
  175.          count--;
  176.          pagecount--;
  177.          fsetpos(infile,¤t[count]);
  178.          cntchar=0;
  179.          break;
  180.          }
  181.           case 'q':
  182.          {                      /* If Quit, append error2 and  */
  183.          errorlevel=2;          /* return to main              */
  184.          return errorlevel;
  185.          }
  186.           case 'Q':
  187.          {
  188.          errorlevel=2;
  189.          return errorlevel;
  190.          }
  191.           default:
  192.          {
  193.          break;
  194.          }
  195.            }
  196.         }
  197.        else
  198.           {
  199.           printf("%c",charstr);       /* Print the char to screen */
  200.           cntchar++;                  /* increment the counter    */
  201.           }
  202.        if (feof(infile))              /* if the EOF has been reached */
  203.           {
  204.           for (loopcount=(21-lincount);loopcount>=0;loopcount--)
  205.           printf("\n");
  206.           printf("\n\33[34;40;1mCurrent File: \33[31;40;1m %s \33[40;37;0m\n",filen);
  207.           printf("\33[37;44;1m\xba Software from Scratch \xba FWD <CR> \xba REV <-> \xba QUIT <q> \xba Page %d <EOF> \xba   ?:\33[40;37;0m",pagecount);
  208.           fflush(stdin);
  209.           readdir=getchar();          /* get in choice */
  210.           fflush(stdin);
  211.           printf("\33[2J");
  212.           switch (readdir)
  213.            {
  214.           case '-':
  215.          {
  216.          if (pagecount==1)
  217.             {
  218.             count++;
  219.             pagecount++;
  220.             }
  221.          count--;
  222.          pagecount--;
  223.          fsetpos(infile,¤t[count]);
  224.          cntchar=0;
  225.          lincount=0;
  226.          break;
  227.          }
  228.           case 'Q':
  229.          {
  230.          doneptr=1;
  231.          break;
  232.          }
  233.           case 'q':
  234.          {
  235.          doneptr=1;
  236.          break;
  237.          }
  238.  
  239.           default :
  240.          {
  241.          fsetpos(infile,¤t[count]);  /* If enter or key+enter */
  242.          cntchar=0;                        /* is pressed, redraw    */
  243.          lincount=0;
  244.          break;
  245.          }
  246.            }
  247.           }
  248.       }
  249.     while (doneptr==0);               /* when finished.....*/
  250.     fclose(infile);                   /*close the infile (give it a shake)*/
  251.     fflush(stdin);                    /*don't forget to flush             */
  252.     errorlevel=0;                     /*assign error 0 */
  253.     return (errorlevel) ;             /* return the errorlevel to main    */
  254.  }
  255.